package com.gxa.service.impl;

import com.gxa.common.R;
import com.gxa.mapper.OrdersMapper;
import com.gxa.pojo.*;
import com.gxa.service.OrdersService;
import com.gxa.utils.GenerateSequenceUtil;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired(required = false)
    private OrdersMapper ordersMapper;
    @Override
    public R queryOrdersList(Integer currentPage, Integer pageSize) {
        R r=new R();
        List list=new ArrayList();
        Integer param1 = (currentPage - 1)*pageSize;
        Integer param2 = pageSize;
        List<Map> map = ordersMapper.queryOrdersList(param1,param2);
        int result=ordersMapper.queryCount();
        list.add(result);
        list.add(map);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return  r;
    }

//    @Override
//    public R search(String customer_name, String consignee_phone, String order_id) {
//        R r=new R();
//        List<Orders> ordersList=ordersMapper.search(customer_name,consignee_phone,order_id);
//        for (Orders orders:ordersList) {
//            if(orders != null){
//                List<Customer> customer =ordersMapper.queryCustomer(orders.getCustomer_id());
//                orders.setCustomer(customer.get(0));
//            }
//        }
//        r.setData(ordersList);
//        r.setCode(200);
//        r.setMsg("搜索成功");
//        return r;
//    }

    @Override
    public R search(String conn,Integer currentPage,Integer pageSize) {
        R r=new R();
        int result=ordersMapper.countByOrders_id(conn);
        if(result>0){
            List list=new ArrayList();
            Integer param1 = (currentPage - 1)*pageSize;
            Integer param2 = pageSize;
            int result1=ordersMapper.queryTotalByOrder_id(conn);
            List<Map> ordersMap=ordersMapper.searchByOrders_id(conn,param1,param2);
            list.add(result1);
            list.add(ordersMap);
            r.setData(list);
            r.setCode(200);
            r.setMsg("搜索成功");
            return  r;
        }else {
            result = ordersMapper.countByContact_phone(conn);
            if (result > 0) {
                List list = new ArrayList();
                Integer param1 = (currentPage - 1) * pageSize;
                Integer param2 = pageSize;
                int result1 = ordersMapper.queryTotalByContact_phone(conn);
                List<Map> ordersMap = ordersMapper.searchByContact_phone(conn, param1, param2);
                list.add(result1);
                list.add(ordersMap);
                r.setData(list);
                r.setCode(200);
                r.setMsg("搜索成功");
                return  r;
            } else {
                result = ordersMapper.countByCustomer_name(conn);
                if (result > 0) {
                    List list = new ArrayList();
                    Integer param1 = (currentPage - 1) * pageSize;
                    Integer param2 = pageSize;
                    int result1 = ordersMapper.queryTotalByCustomer_name(conn);
                    List<Map> ordersMap = ordersMapper.searchByCustomer_name(conn, param1, param2);
                    list.add(result1);
                    list.add(ordersMap);
                    r.setData(list);
                    r.setCode(200);
                    r.setMsg("搜索成功");
                    return  r;
                }else {
                    int result1=0;
                    r.setData(result1);
                    r.setMsg("搜索失败");
                    return  r;
                }
            }

        }

    }
    @Override
    public R deleteById(String order_id,Integer status) {
        R r=new R();
        int result=ordersMapper.deleteByOrder_id(order_id,status);
        if(result>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }
        return r;
    }

    @Override
    public R updateStatus(Integer status, String order_id) {
        R r = new R();
        Integer resoult = ordersMapper.updateStatus(status,order_id);
        if (resoult>0){
            r.setCode(200);
            r.setMsg("修改成功");
        }
        return r;
    }

    @Override
    public R updateShippingStatus(Integer shippingStatus,String order_id) {
        R r = new R();
        Integer resoult = ordersMapper.updateShippingStatus(shippingStatus,order_id);
        if (resoult>0){
            r.setCode(200);
            r.setMsg("修改成功");
        }
        return r;
    }



    @Override
    public R queryByName(Integer customer_id,Integer currentPage,Integer pageSize ) {
        R r=new R();
        List list=new ArrayList();
        Integer param1 = (currentPage - 1)*pageSize;
        Integer param2 = pageSize;
        int result=ordersMapper.queryTotalResult(customer_id);
        List<Map> ordersMap=ordersMapper.queryByName(customer_id,param1,param2);
        list.add(result);
        list.add(ordersMap);
        r.setCode(200);
        r.setData(list);
        return r;
    }


    @Override
    public R updateReceived(Integer received, String order_id) {
        R r = new R();
        Integer resoult = ordersMapper.updateReceived(received,order_id);
        if (resoult>0){
            r.setCode(200);
            r.setMsg("修改成功");
        }
        return r;
    }

    @Override
    public R queryByOrder_id(String order_id) {
        R r=new R();
        List<Map> maps=ordersMapper.queryById(order_id);
        r.setCode(200);
        r.setData(maps);
        return r;
    }

    @Override
    public R queryByTodayOrder_date(Integer currentPage,Integer pageSize) {
        R r=new R();
        List list=new ArrayList();
        Integer param1 = (currentPage - 1)*pageSize;
        Integer param2 = pageSize;
        List<Map> map = ordersMapper.queryByOrder_date(param1,param2);
        int result=ordersMapper.queryTotalSize();
        list.add(map);
        list.add(result);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryWeekByOrder_date(Integer currentPage,Integer pageSize) {
        R r=new R();
        List list=new ArrayList();
        Integer param1 = (currentPage - 1)*pageSize;
        Integer param2 = pageSize;
        List<Map> maps=ordersMapper.queryByDate(param1,param2);
        int result=ordersMapper.queryTotal();
        list.add(maps);
        list.add(result);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryMonthByOrder_date(Integer currentPage,Integer pageSize) {
        R r=new R();
        List list=new ArrayList();
        Integer param1 = (currentPage - 1)*pageSize;
        Integer param2 = pageSize;
        List<Map> maps=ordersMapper.queryMonth(param1,param2);
        int result=ordersMapper.querySize();
        list.add(maps);
        list.add(result);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryOrders(Integer currentPage,Integer pageSize) {
        R r=new R();
        List list=new ArrayList();
        Integer params1 = (currentPage - 1)*pageSize;
        Integer params2 = pageSize;
        List<Map> maps=ordersMapper.queryOrders(params1,params2);
        int result=ordersMapper.queryResult();
        list.add(maps);
        list.add(result);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R searchAddOrders(String argument,Integer currentPage,Integer pageSize) {
        R r=new R();
        int result=ordersMapper.countByCustomer(argument);
        if(result>0){
            Integer param1 = (currentPage - 1)*pageSize;
            Integer param2 = pageSize;
            List<Map> ordersMap=ordersMapper.searchByCustomer(argument,param1,param2);
            r.setData(ordersMap);
            r.setCode(200);
            r.setMsg("搜索成功");
        }else {
            result = ordersMapper.countByContact(argument);
            if (result > 0) {
                Integer param1 = (currentPage - 1)*pageSize;
                Integer param2 = pageSize;
                List<Map> ordersMap = ordersMapper.searchByContact(argument,param1,param2);
                r.setData(ordersMap);
                r.setCode(200);
                r.setMsg("搜索成功");
            } else {
                result = ordersMapper.countByContact_name(argument);
                if (result > 0) {
                    Integer param1 = (currentPage - 1)*pageSize;
                    Integer param2 = pageSize;
                    List<Map> ordersMap = ordersMapper.searchByContact_name(argument,param1,param2);
                    r.setData(ordersMap);
                    r.setCode(200);
                    r.setMsg("搜索成功");
                } else {
                    r.setCode(500);
                    r.setMsg("搜索失败");
                }
            }
        }
        return r;
    }

    @Override
    public R updatreByOrders_Id(Map map) {
        R r=new R();
        Integer result=ordersMapper.updateByOrder_Id(map);
        r.setCode(200);
        r.setData(result);
        r.setMsg("修改成功");
        return r;
    }

    @Override
    public R countOrdersByEmployee(String start_time, String stop_time) {
        List<Employee> empList=ordersMapper. queryEmployee();
        Map<String,Integer> result=new HashMap<>();
        for (Employee employee:empList) {
            String employee_name=employee.getEmployee_name();
            Integer count=ordersMapper.countOrdersByEmployee(employee_name,start_time,stop_time);
            result.put(employee_name,count);
        }
        R r= new R();
        r.setCode(200);
        r.setData(result);
        r.setMsg("查询成功");
        return r;
    }

    @Override
    public R countOrdersByDept(String start_time, String stop_time) {
        Map<String,Integer> result=new HashMap<>();
        List<Dept> deptList=ordersMapper.queryDept();
        for (Dept dept:deptList) {
            String dept_name=dept.getDept_name();
            Integer count=ordersMapper.countOrdersByDept(dept_name,start_time,stop_time);
            result.put(dept_name,count);
        }
        R r= new R();
        r.setCode(200);
        r.setData(result);
        r.setMsg("查询成功");
        return r;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R add(Map map) {
        R r=new R();
        String orderid=GenerateSequenceUtil.generateSequenceNo();
        map.put("order_id",orderid);
        Integer result=ordersMapper.add(map);
        if(result>0){
            r.setData(orderid);
            r.setCode(200);
            r.setMsg("新增成功");
        }

        return r;
    }


    @Override
    public R queryOrderGoods(String order_id,Integer currentPage,Integer pageSize) {
        R r = new R();
        Integer params1 =(currentPage-1)*pageSize;
        Integer params2 = pageSize;
        List list = new ArrayList();
        List<Map> mapper1 = ordersMapper.queryOrderGoods(order_id,params1,params2);
        int resoult = ordersMapper.queryAllOrdersGoods(order_id);
        list.add(resoult);
        list.add(mapper1);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    public R addGoodOrder(String order_id, Integer goods_id) {
        R r = new R();
        Integer resoult = ordersMapper.addGoodOrder(order_id,goods_id);
        if (resoult>0){
            r.setCode(200);
            r.setMsg("添加成功");
        }
        return r;
    }
    public R deleteOrderGoods(String order_id,Integer order_goods_id,Integer order_goods_status) {
        R r = new R();
        Integer resoult = ordersMapper.deleteOrderGoods(order_id,order_goods_id,order_goods_status);
        if (resoult>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }
        return r;
    }
    public R queryGoods(Integer currentPage, Integer pageSize) {
        R r = new R();
        Integer params1 =(currentPage-1)*pageSize;
        Integer params2 = pageSize;
        List<Map> goodsList = ordersMapper.queryGoodsList(params1,params2);
        Integer resoult = ordersMapper.queryGoods();
        List list = new ArrayList();
        list.add(goodsList);
        list.add(resoult);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R updateOrderGoods(Map map) {
        R r = new R();
        System.out.println(map);
        Integer resoult = ordersMapper.updateOrderGoods(map);
        r.setCode(200);
        r.setMsg("修改成功");
        r.setData(resoult);
        return r;
    }


    @Override
    public R findAll(){
        R r = new R();
        List<Map> all = ordersMapper.findAll();
        List arrList1 = new ArrayList();
        for(Map map : all ){
            Integer employe_id =(Integer) map.get("employee_id");
            Integer A = ordersMapper.findNumber(employe_id);
            Integer B =ordersMapper.findNumberStatus(employe_id);
            List<Map> C = ordersMapper.findMonye(employe_id);
            Map s = new HashMap();
            s.put("all" ,A);
            s.put("allStatus" ,B);
            s.put("money" ,C);
            arrList1.add(s);
        }
        arrList1.add(all);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(arrList1);
        return r;
    }

    public R deleteOrdersBycustomerId(Integer customer_Id){
        R r = new R();
        Integer resoult1 = ordersMapper.deletecustomerorders(customer_Id);
        List<String> orderAllId = ordersMapper.findOrders(customer_Id);
        for(String orderId : orderAllId){
            ordersMapper.deleteCustomerOrderGoods(orderId);
        }
        if (resoult1>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }
        return r;
    }



}
